package com.canaan.business.service.inOutStock.impl;

import java.util.Date;
import java.util.List;
import com.canaan.common.utils.DateUtils;
import com.canaan.common.utils.SecurityUtils;
import com.canaan.common.utils.TableNoUtil;
import com.canaan.common.utils.uuid.UUID;
import com.canaan.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import com.canaan.business.mapper.inOutStock.InStockBillMapper;
import com.canaan.business.domain.inOutStock.InStockBill;
import com.canaan.business.service.inOutStock.IInStockBillService;
import com.canaan.common.annotation.TableName;
/**
 * 入库单Service业务层处理
 *
 * @author canaan
 * @date 2025-03-29
 */
@Service
public class InStockBillServiceImpl implements IInStockBillService {
    @Resource
    private InStockBillMapper inStockBillMapper;

    /**
     * 查询入库单
     *
     * @param id 入库单主键
     * @return 入库单
     */
    @Override
    public InStockBill selectInStockBillById(Long id) {
        return inStockBillMapper.selectInStockBillById(id);
    }

    /**
     * 查询入库单列表
     *
     * @param inStockBill 入库单
     * @return 入库单
     */
    @Override
    public List<InStockBill> selectInStockBillList(InStockBill inStockBill) {
        return inStockBillMapper.selectInStockBillList(inStockBill);
    }

    /**
     * 新增入库单
     *
     * @param inStockBill 入库单
     * @return 结果
     */
    @Override
    public int insertInStockBill(InStockBill inStockBill) {
        if(StringUtils.isBlank(inStockBill.getNo())){
            inStockBill.setNo("IN"+TableNoUtil.getNo(InStockBill.class));
        }
        inStockBill.setCreateBy(SecurityUtils.getUsername());
        inStockBill.setCreateTime(DateUtils.getNowDate());
        return inStockBillMapper.insertInStockBill(inStockBill);
    }

    /**
     * 批量新增入库单
     *
     * @param inStockBillList 入库单列表
     * @return 结果
     */
    @Override
    public int batchInsertInStockBill(List<InStockBill> inStockBillList) {
        String username = SecurityUtils.getUsername();
        Date currerTime = DateUtils.getNowDate();
        for (InStockBill inStockBill : inStockBillList){
            if(StringUtils.isBlank(inStockBill.getNo())){
                inStockBill.setNo("IN"+TableNoUtil.getNo(InStockBill.class));
            }
            inStockBill.setCreateBy(username);
            inStockBill.setCreateTime(currerTime);
        }
        int insertNum = 0;
        for (int i=0; i<inStockBillList.size();) {
            int endIndex = i+500;
            if (endIndex > inStockBillList.size()) {
                endIndex = inStockBillList.size();
            }
            insertNum = insertNum + inStockBillMapper.batchInsertInStockBill(inStockBillList.subList(i, endIndex));
            i = endIndex;
        }
        return insertNum;
    }

    /**
     * 修改入库单
     *
     * @param inStockBill 入库单
     * @return 结果
     */
    @Override
    public int updateInStockBill(InStockBill inStockBill) {
        if(StringUtils.isEmpty(inStockBill.getUpdateBy())) {
            inStockBill.setUpdateBy(SecurityUtils.getUsername());
        }
        inStockBill.setUpdateTime(DateUtils.getNowDate());
        return inStockBillMapper.updateInStockBill(inStockBill);
    }

    /**
     * 批量修改入库单
     *
     * @param inStockBillList 入库单列表
     * @return 结果
     */
    @Override
    public int batchUpdateInStockBill(List<InStockBill> inStockBillList) {
        String username = SecurityUtils.getUsername();
        Date currerTime = DateUtils.getNowDate();
        for (InStockBill inStockBill : inStockBillList){
            inStockBill.setUpdateBy(username);
            inStockBill.setUpdateTime(currerTime);
        }
        int updateNum = 0;
        for (int i=0; i<inStockBillList.size();) {
            int endIndex = i+500;
            if (endIndex > inStockBillList.size()) {
                endIndex = inStockBillList.size();
            }
            updateNum = updateNum + inStockBillMapper.batchUpdateInStockBill(inStockBillList.subList(i, endIndex));
            i = endIndex;
        }
        return updateNum;
    }

    /**
     * 批量删除入库单
     *
     * @param ids 需要删除的入库单主键集合
     * @return 结果
     */
    @Override
    public int deleteInStockBillByIds(Long[] ids) {
        return inStockBillMapper.deleteInStockBillByIds(ids);
    }

    /**
     * 删除入库单信息
     *
     * @param id 入库单主键
     * @return 结果
     */
    @Override
    public int deleteInStockBillById(Long id) {
        return inStockBillMapper.deleteInStockBillById(id);
    }

    /**
     * 查询入库单
     *
     * @param no 编号
     * @return 入库单
     */
    @Override
    public InStockBill selectInStockBillByNo(String no) {
        return inStockBillMapper.selectInStockBillByNo(no);
    }

    /**
     * 批量删除入库单
     *
     * @param nos 需要删除的编号集合
     * @return 结果
     */
    @Override
    public int deleteInStockBillByNos(String[] nos) {
        return inStockBillMapper.deleteInStockBillByNos(nos);
    }

    /**
     * 删除入库单信息
     *
     * @param no 编号
     * @return 结果
     */
    @Override
    public int deleteInStockBillByNo(String no) {
        return inStockBillMapper.deleteInStockBillByNo(no);
    }
}
